package com.luoguijia.disk.files.repository;

import com.luoguijia.disk.files.entity.FileEntity;
import com.luoguijia.disk.files.entity.SearchFileEntity;
import com.luoguijia.disk.mybatis.entity.PtFileEntity;
import com.luoguijia.disk.mybatis.flexigrid.PagingEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Delete;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository // 新增这个注解
public interface FileDao {

    List<PtFileEntity> queryFileList();


    // 逻辑删除文件（更新为回收站状态）
    @Update("UPDATE file_table " +
            "SET is_deleted = 1, recycle_time = #{recycleTime} " +
            "WHERE id = #{id} AND is_deleted = 0") // 防止重复删除
    int logicDelete(FileEntity file);

    // 其他方法：如查询文件列表、回收站列表等，根据需求补充
    // 原有方法...

    // 新增搜索方法
    @Select("SELECT id, name, path, size, create_time as createTime, " +
            "CASE WHEN file_type = 1 THEN '' ELSE SUBSTRING_INDEX(name, '.', -1) END as fileType, " +
            "file_type = 1 as isFolder " +
            "FROM file_table " +
            "WHERE user_id = #{userId} AND is_deleted = 0 AND name LIKE CONCAT('%', #{keyword}, '%') " +
            "ORDER BY isFolder DESC, name ASC " +
            "LIMIT #{offset}, #{pageSize}")
    List<SearchFileEntity> searchFiles(@Param("userId") String userId,
                                       @Param("keyword") String keyword,
                                       @Param("offset") Integer offset,
                                       @Param("pageSize") Integer pageSize);

    @Select("SELECT COUNT(1) FROM file_table " +
            "WHERE user_id = #{userId} AND is_deleted = 0 AND name LIKE CONCAT('%', #{keyword}, '%')")
    Integer countSearchFiles(@Param("userId") String userId,
                             @Param("keyword") String keyword);


    /**
     * 删除文件表的所有内容
     */
    void deleteFileAll();

    /**
     * 保存文件信息
     * @param ptFileEntityList
     */
    void saveFileInfo(@Param("list") List<PtFileEntity> ptFileEntityList);

    // 文件查询方法
    @Select("SELECT id, file_path as filePath, file_type as fileType, file_name as fileName " +
            "FROM file_table WHERE id = #{fileId} AND is_deleted = 0")
    PtFileEntity getFileById(@Param("fileId") String fileId);

    /**
     * 查询文件列表总数量
     * @param entity
     * @return
     */
    Integer queryFileTotal(@Param("entity") SearchFileEntity entity);

    /**
     * 文件列表分页查询
     * @param entity
     * @param paging
     * @return
     */
    List<PtFileEntity> queryFileInfoListByPage(@Param("entity") SearchFileEntity entity, @Param("paging") PagingEntity paging);
/*

    */
/**
     * 查询文件信息
     * @param fileUuid
     * @return
     *//*

    PtFileEntity queryFileInfoById(@Param("fileUuid") String fileUuid);
*/

    /**
     * 查询所有文件列表
     * @return
     */
    List<PtFileEntity> queryFileListAll();

    /**
     * 查询回收站文件总数
     * @param entity
     * @return
     */
    Integer queryRecycleFileTotal(@Param("entity") SearchFileEntity entity);

    /**
     * 分页查询回收站文件列表
     * @param entity
     * @param paging
     * @return
     */
    List<PtFileEntity> queryRecycleFileInfoListByPage(@Param("entity") SearchFileEntity entity, @Param("paging") PagingEntity paging);

    /**
     * 从回收站恢复文件
     * @param fileId
     * @return
     */
    @Update("UPDATE file_table SET is_deleted = 0 WHERE id = #{fileId} AND is_deleted = 1")
    int restoreFileFromRecycle(@Param("fileId") String fileId);

    /**
     * 永久删除文件
     * @param fileId
     * @return
     */
    @Delete("DELETE FROM file_table WHERE id = #{fileId} AND is_deleted = 1")
    int deleteFile(@Param("fileId") String fileId);

    /* 需要添加import语句: import org.apache.ibatis.annotations.Delete; */

    /**
     * 批量删除角色
     * @param fileArray
     */
    void deleteFileByIds(@Param("fileArray") String[] fileArray);


    /**
     * 查询文件信息
     * @param fileUuid
     * @return
     */
    PtFileEntity queryFileInfoById(@Param("fileUuid") String fileUuid);


    /**
     * 修改文件信息q
     * @param entity
     */
    void modifyFileInfoById(@Param("entity") PtFileEntity entity);



}


